package com.dw.etl.core.service;

import com.dw.etl.common.util.UUIDUtil;
import  com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.BaseDimtbService;
import com.dw.etl.core.api.MdMedTbFdService;
import com.dw.etl.core.api.MdMedTbService;
import com.dw.etl.core.bean.model.*;
import com.dw.etl.core.mapper.BaseIndexConfFdMapper;
import com.dw.etl.core.mapper.BaseIndexConfTbMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.dw.etl.core.api.BaseIndexConfService;
import com.dw.etl.core.mapper.BaseIndexConfMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * baseIndexConf服务实现类
 * Created by hozeData on 2021/06/21.
 */
@Service
@Transactional
public class BaseIndexConfServiceImpl implements BaseIndexConfService{

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseIndexConfServiceImpl.class);

    @Autowired
    private BaseIndexConfMapper baseIndexConfMapper;

    @Autowired
    private BaseIndexConfTbMapper baseIndexConfTbMapper;

    @Autowired
    private BaseIndexConfFdMapper baseIndexConfFdMapper;

    @Autowired
    private BaseDimtbService baseDimtbService;

    @Autowired
    private MdMedTbService mdMedTbService;

    @Autowired
    private MdMedTbFdService mdMedTbFdService;

    @Override
    public int insert(BaseIndexConf baseIndexConf) {
        if(StringUtils.isEmpty(baseIndexConf.getPk())){
            String uuid32 = UUIDUtil.getUUID32();
            baseIndexConf.setPk(uuid32);
        }
        return baseIndexConfMapper.insert(baseIndexConf);
    }

    @Override
    public int insertSelective(BaseIndexConf baseIndexConf)  {
        if(StringUtils.isEmpty(baseIndexConf.getPk())){
            String uuid32 = UUIDUtil.getUUID32();
            baseIndexConf.setPk(uuid32);
        }
        return baseIndexConfMapper.insertSelective(baseIndexConf);
    }

    @Override
    public int deleteById(String pk)  {
        return baseIndexConfMapper.deleteByPrimaryKey(pk);
    }

    @Override
    public int updateByIdSelective(BaseIndexConf baseIndexConf)  {

        return baseIndexConfMapper.updateByPrimaryKeySelective(baseIndexConf);
    }

    @Override
    public int updateById(BaseIndexConf baseIndexConf)  {
        return baseIndexConfMapper.updateByPrimaryKey(baseIndexConf);
    }

    @Override
    public BaseIndexConf getById(String pk)  {
        return baseIndexConfMapper.selectByPrimaryKey(pk);
    }

    @Override
    public List<BaseIndexConf> findByModel(BaseIndexConf baseIndexConf)  {
        return baseIndexConfMapper.findByModel(baseIndexConf);
    }

    @Override
    public PaginationVO<BaseIndexConf> findPageByModel(BaseIndexConf baseIndexConf, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseIndexConf> quesList = baseIndexConfMapper.findByModel(baseIndexConf);
        PageInfo<BaseIndexConf> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseIndexConf> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    /**
     * 样例sql
     * select 维度集,指标,dimDate from 主表
     * inner join 从表1 on 主表.字段=从表1.字段
     * ...
     * inner join 维表1 on 主(从)表.字段=维表1.字段
     * where 条件
     * group by 维度集,dimDate
     *
     * 获取全部维度集去重，记录主表与join表（事实表、维度表）、where条件
     * @return
     */
     public List<String> findAllSql() {
         List<BaseIndexConf> indexConfList = this.findByModel(new BaseIndexConf());
         List<BaseIndexConfTb> confTbList;
         List<BaseIndexConfFd> confFdList;
         List<String> dimFldList = new ArrayList<>();
         List<String> sqlList = new ArrayList<>();
         BaseDimtb dimtb;
         for(BaseIndexConf conf:indexConfList) {
             BaseIndexConfTb tbModel = new BaseIndexConfTb();
             tbModel.setPkConf(conf.getPk());
             confTbList = baseIndexConfTbMapper.findByModel(tbModel);
             BaseIndexConfFd fdModel = new BaseIndexConfFd();
             fdModel.setPkConf(conf.getPk());
             confFdList = baseIndexConfFdMapper.findByModel(fdModel);
             String dimFields = "",mainTable = "",joinTable = "",sql="";
             for(BaseIndexConfTb tb:confTbList) {
                 MdMedTbFd tbFdModel = new MdMedTbFd();
                 tbFdModel.setFgDim((byte)1);
                 tbFdModel.setFgAct((byte)1);
                 tbFdModel.setPkPubfd(tb.getPkTb());
                 List<MdMedTbFd> tbFdList = mdMedTbFdService.findByModel(tbFdModel);
                 for(MdMedTbFd fd:tbFdList) {
                     if(!dimFldList.contains(fd.getPkPubfd())) {
                         dimFields += fd.getIdMedTb()+"."+fd.getPkPubfd()+",";
                         dimFldList.add(fd.getPkPubfd());
                     }
                 }
                 if(StringUtils.isEmpty(tb.getSqlOn())) {
                     mainTable = tb.getPkTb();
                 } else {
                     joinTable = " inner join "+tb.getPkTb()+" on "+tb.getSqlOn();
                 }
             }
             for(BaseIndexConfFd fd:confFdList) {
                 dimtb = baseDimtbService.getByIdFromCache(fd.getPkPubfd());
                 joinTable = " inner join "+dimtb.getNaDimtb()+" on "+fd.getPkTb()+"."+fd.getPkPubfd()+"="+dimtb.getNaDimtb()+".pk";

             }
             if(StringUtils.isNotEmpty(mainTable)) {
                 sql = "select "+dimFields+conf.getPkIndex()+" as pk_index"+conf.getNaIndex()+" as na_index "+conf.getSqlVal()+" as value "+ conf.getGrpDayfld()+" as dim_date from "
                         + mainTable+joinTable+conf.getSqlWhere()
                         +" group by "+dimFields+" "+conf.getGrpDayfld();
                 sqlList.add(sql);
             }
         }
         return sqlList;
     }



}
